Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
How to force update PWA cache after login [JS]?

I have a simple forum that I want to make work offline. I have a dynamic and static cahce. The static cache fills on the install event and the dynamic as you go allong and look at posts. The problem is that the pages it caches include the header where you have a link to the profile if you are logged in and link to registration page if you are not logged in. After logging in it still shows the registration link instead of the profile link. The way to fix it would be to refresh the cache?

Is there a way to do this or is there some other fix for this type of issue(besides network first approach)? I am relativly new to PWAs and I can't find any useful tips.

My service worker looks like this:

const staticCacheName = "ScroocCacheV1";
const dynamicCacheName = "ScroocDynamicCacheV1";

const assets = [
        '/',
        '/css/main_styles.css',
        '/js/ui.js',
        '/about',
        '/policies',
        '/register',
        '/createTopic',
        '/stats',
        '/proposals',
];

const limitCacheSize = (name, size) => {
    caches.open(name).then(cache => {
        cache.keys().then(keys => {
            if(keys.length > size) {
                cache.delete(keys[0]).then(limitCacheSize(name, size));
            }
        });
    });
}
const dynamicCacheLimit = 18;

// Install service worker
self.addEventListener('install', evt => {
    evt.waitUntil(
        caches.open(staticCacheName).then(cache => {
            cache.addAll(assets);
        })
    );
});

// Activate event
self.addEventListener('activate', evt => {
    evt.waitUntil(
        caches.keys().then(keys => {
            keys.map((key => {
                if (key !== staticCacheName && key !== dynamicCacheName) {
                    return caches.delete(key); //Deleting the old cache (cache v1)
                }
            }))
        })
    )
});

// Intercept fetch 
self.addEventListener('fetch', evt => {
    evt.respondWith(
        fetch(evt.request).then(fetchRes => {
            return caches.open(dynamicCacheName).then(cache => {
                return caches.match(evt.request).then(function(result) {
                    if (result) {
                        return result;
                    } else {
                        cache.put(evt.request.url, fetchRes.clone());
                        limitCacheSize(dynamicCacheName, dynamicCacheLimit);
                        return fetchRes; 
                    }
                });
            });
        }).catch(function() {
            return caches.match(evt.request).catch((error) => {
                console.log(error)
                return caches.match('/img/fallbackImage.png');
            });
        })        
    );
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This worked for me!

Before you could attempt to empty the cache, the service worker must first be successfully installed. So for the record, your sw.js file should begin with the usual

self.addEventListener("install", ...etc

Now this is where we get to cleaning up. Create a variable to store the name of the cache you wish to purge/update (makes targeting different caches easier)

var TargetCache= 'NameOfCacheToClean';

Next, add an EventListener that triggers each time the service worker is activated (the activate-event occurs on page reload/refresh)

self.addEventListener('activate', event => 
{
  const currentCaches = [TargetCache];
  event.waitUntil
  (
    caches.keys()
    .then(cacheNames => {return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));})
    .then(cachesToDelete => {return Promise.all(cachesToDelete.map(cacheToDelete => {return caches.delete(cacheToDelete);})); })
    .then(() => self.clients.claim())
  );
});

Just in-case, i normally add the event listener that intercepts the outgoing fetch-requests after the code that clears the old cache.

self.addEventListener('fetch', function(event) {...etc
about 4 years ago · Juan Pablo Isaza Relatório

0

The way to fix it would be to refresh the cache?

That is correct, assuming you cached the path /login, the service worker will always display what was cached under that path, based on your code.

Is there a way to do this or is there some other fix for this type of issue(besides network first approach)?

It's not really something to "fix", what you described is somewhat expected behaviour.

There are several ways around this tho, network first is just one:

  • use a message to update cache on login
  • use different urls or url parts like query to skip cache when user is logged in
  • hide the UI that you don't need on the client depending on user state

Probably many more.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda